package com.talkweb.ncfw.web.action;

import java.util.ArrayList;
import java.util.List;

import com.talkweb.ncfw.component.Constants;
import com.talkweb.ncfw.component.rights.entity.Func;
import com.talkweb.ncfw.component.rights.entity.FuncGroup;
import com.talkweb.ncfw.component.rights.service.IFuncManager;
import com.talkweb.ncfw.data.DataMap;
import com.talkweb.ncfw.data.IDataMap;
import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.data.ProcessResult;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.ViewBaseException;
import com.talkweb.ncfw.security.SecurityHelper;
import com.talkweb.ncfw.utils.StringUtils;
import com.talkweb.ncfw.web.struts2.JqGridBaseAction;
import com.talkweb.ncfw.web.utils.HttpServletUtils;
import com.talkweb.ncfw.web.utils.json.JsonUtils;
import com.talkweb.ncfw.web.utils.tree.TreeBean;

/**
 * <p>文件名称: FuncAction.java</p>
 * <p>文件描述: 系统功能Action</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>公　　司: 拓维信息系统股份有限公司</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2011-5-3</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class FuncAction extends JqGridBaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1599650330892032910L;
	
	private IFuncManager funcManager;

	public IFuncManager getFuncManager() {
		return funcManager;
	}

	public void setFuncManager(IFuncManager funcManager) {
		this.funcManager = funcManager;
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：获取功能列表
	 * @return
	 * @throws ViewBaseException
	 */
	public String listFunc () throws ViewBaseException {
		Page<Func> page = this.getPage();
		IDataMap<String, Object> condition = this.getParameterMap(); 
		try {
			page = this.funcManager.findFunc(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			this.bindDataGrid(page);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：录入功能组信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String inputFuncGroup () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String dataJsonStr = this.getParameter("data");		//功能组json字符串
		FuncGroup funcGroup = (FuncGroup)JsonUtils.getDTO(dataJsonStr, FuncGroup.class);
		if (StringUtils.isEmpty(funcGroup.getGroupid())) {//新增功能组
			try {
				returnCode = this.funcManager.addFuncGroup(funcGroup);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "功能组添加成功" : "功能组添加失败";
		} else {//修改功能组
			try {
				returnCode = this.funcManager.updateFuncGroup(funcGroup);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "功能组信息修改成功" : "功能组信息修改失败";
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：录入功能信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String inputFunc () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String dataJsonStr = this.getParameter("data");		//功能json字符串
		Func func = (Func)JsonUtils.getDTO(dataJsonStr, Func.class);
		if (StringUtils.isEmpty(func.getFuncid())) {//新增功能
			try {
				returnCode = this.funcManager.addFunc(func);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "功能添加成功" : "功能添加失败";
		} else {//修改功能
			try {
				returnCode = this.funcManager.updateFunc(func);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "功能信息修改成功" : "功能信息修改失败";
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：删除功能组
	 * @return
	 * @throws ViewBaseException
	 */
	public String deleteFuncGroup () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String groupid = this.getParameter("groupid");
		try {
			returnCode = this.funcManager.deleteFuncGroupByGroupid(groupid);
			flag = (returnCode == 1);
			msg = flag ? "功能组删除成功" : "功能组删除失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：删除功能
	 * @return
	 * @throws ViewBaseException
	 */
	public String deleteFunc () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String funcids = this.getParameter("funcids");
		String[] funcidArray = null;
		if (!StringUtils.isEmpty(funcids)) {
			funcidArray = funcids.split(",");
		}
		try {
			returnCode = this.funcManager.deleteFuncByFuncid(funcidArray);
			flag = (returnCode == 1);
			msg = flag ? "功能删除成功" : "功能删除失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-5-3
	 * @Description：获取单个功能的详细信息
	 * @return
	 */
	public String getFuncGroup () throws ViewBaseException {
		String groupid = this.getParameter("groupid");
		FuncGroup funcGroup = null;
		try {
			funcGroup = this.funcManager.getFuncGroupByGroupid(groupid);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), funcGroup);
		}
		return null;
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-5-3
	 * @Description：获取单个功能的详细信息
	 * @return
	 */
	public String getFunc () throws ViewBaseException {
		String funcid = this.getParameter("funcid");
		Func func = null;
		try {
			func = this.funcManager.getFuncByFuncid(funcid);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), func);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-26
	 * @Description：查询功能组树
	 * @return
	 * @throws ViewBaseException
	 */
	public String findFuncGroupSupportTree () throws ViewBaseException {
		IDataMap<String, Object> condition = this.getParameterMap();
		
		if (!SecurityHelper.hasSysadminRight()) {
			
		}
		
		//查询功能组
		Page<FuncGroup> funcGroupPage = new Page<FuncGroup>();
		funcGroupPage.setAutoCount(false);
		funcGroupPage.setIfPage(false);
		funcGroupPage.setOrderBy("sortNo");
		funcGroupPage.setOrder(Page.ASC);
		try {
			funcGroupPage = this.funcManager.findFuncGroup(funcGroupPage, condition);
		} catch (BizBaseException e1) {
			throw new ViewBaseException(e1.getMessage(), e1);
		}
		List<FuncGroup> funcGroupList = funcGroupPage.getResult();
		List<TreeBean> treeBeanList = new ArrayList<TreeBean>();
		if (funcGroupList != null && funcGroupList.size() > 0) {
			for (FuncGroup funcGroup : funcGroupList) {
				TreeBean treeBean = new TreeBean();
				treeBean.setId(funcGroup.getGroupid());
				if (StringUtils.isEmpty(funcGroup.getPgroupid())) {
					treeBean.setParentid(Constants.RightConst.FUNCGROUP_ROOT_ID);
				} else {
					treeBean.setParentid(funcGroup.getPgroupid());
				}
				treeBean.setText(funcGroup.getGroupname());
				treeBean.setExpanded(false);
				treeBeanList.add(treeBean);
			}
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJsonArray(this.getResponse(), treeBeanList);
		}
		return null;
	}

	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：创建功能树
	 * @return
	 * @throws ViewBaseException
	 */
	public String createFuncTree () throws ViewBaseException {
		//请求系统功能数据
		IDataMap<String, Object> condition = new DataMap<String, Object>();
		if (!SecurityHelper.hasSysadminRight()) {
			
		}
		
		//查询功能组
		Page<FuncGroup> funcGroupPage = new Page<FuncGroup>();
		funcGroupPage.setAutoCount(false);
		funcGroupPage.setIfPage(false);
		funcGroupPage.setOrderBy("sortNo");
		funcGroupPage.setOrder(Page.ASC);
		try {
			funcGroupPage = this.funcManager.findFuncGroup(funcGroupPage, condition);
		} catch (BizBaseException e1) {
			throw new ViewBaseException(e1.getMessage(), e1);
		}
		List<FuncGroup> funcGroupList = funcGroupPage.getResult();
		List<TreeBean> treeBeanList = new ArrayList<TreeBean>();
		if (funcGroupList != null && funcGroupList.size() > 0) {
			for (FuncGroup funcGroup : funcGroupList) {
				TreeBean treeBean = new TreeBean();
				treeBean.setId("group~" + funcGroup.getGroupid());
				if (StringUtils.isEmpty(funcGroup.getPgroupid())) {
					treeBean.setParentid(Constants.RightConst.FUNCGROUP_ROOT_ID);
				} else {
					treeBean.setParentid("group~" + funcGroup.getPgroupid());
				}
				treeBean.setText(funcGroup.getGroupname());
				treeBean.setExpanded(false);
				treeBeanList.add(treeBean);
			}
		}
		
		//请求系统功能数据
		IDataMap<String, Object> funcCondition = new DataMap<String, Object>();
		if (!SecurityHelper.hasSysadminRight()) {
			
		}
		
		//查询功能
		Page<Func> page = new Page<Func>();
		page.setAutoCount(false);
		page.setIfPage(false);
		page.setOrderBy("sortNo");
		page.setOrder(Page.ASC);
		try {
			page = this.funcManager.findFunc(page, funcCondition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		List<Func> funcList = page.getResult();
		if (funcList != null && funcList.size() > 0) {
			for (Func func : funcList) {
				TreeBean treeBean = new TreeBean();
				treeBean.setId(func.getFuncid());
				treeBean.setParentid("group~" + func.getGroupid());
				treeBean.setText(func.getFuncname());
				treeBean.setExpanded(false);
				treeBeanList.add(treeBean);
			}
		}
		TreeBean root = new TreeBean();
		String treeRootId = Constants.RightConst.FUNCGROUP_ROOT_ID;
		root.setId(treeRootId);
		root.setText("功能管理");
		root.setParentid("0");
		treeBeanList.add(root);
		HttpServletUtils.outJsonArray(this.getResponse(), treeBeanList);
		return null;
	}
	
}

